package com.yoomiito.app.ui.my.remain.submit;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.yoomiito.app.R;
import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseLazyFragmentPresenter;
import com.yoomiito.app.model.my.MyRemainMoneyAgentInfo;
import com.yoomiito.app.model.my.MyRemainMoneyInfo;
import com.yoomiito.app.model.my.MyServicePrecent;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.utils.encrypt.AESUtils;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.YMToast;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;

public class RemainPresenter extends BaseLazyFragmentPresenter<RemainFragment> {


    public RemainPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void submitData(String money,String psw) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("money",money);
        String json = AESUtils.rsaEncrypt(jsonObject.toJSONString());
        String json1 = AESUtils.rsaEncrypt(psw);
        if (TextUtils.isEmpty(json)||TextUtils.isEmpty(json1)){
            getV().rsaPswError();
            YMToast.showText("提现失败，请重试");
            return;
        }
        YmApi.getYmService()
                .withdraw(json,json1)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<Object>getApiTransformerData())
                .compose(XApi.<Object>getScheduler())
                .subscribe(new ApiSubscriber<Object>() {
                    @Override
                    public void onSuccess(Object data) {
                        getV().submitSuccess();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                        if (error.getCode() == ApiException.PAY_PSW_ERROR) {
                            getV().error(true, ResUtil.getString(R.string.psw_error));
                        } else {
                            getV().error(false,"");
                            YMToast.showText(error.getMsg());
                        }
                    }
                });
    }

    //获取服务费
    public void getWithDrawServicePresent(String money) {
        YmApi.getYmService()
                .getWithdrawServicePrecent(money)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<MyServicePrecent>getApiTransformerData())
                .compose(XApi.<MyServicePrecent>getScheduler())
                .subscribe(new ApiSubscriber<MyServicePrecent>() {
                    @Override
                    public void onSuccess(MyServicePrecent data) {
                        getV().showSureDialog(data);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        YMToast.showText(error.getMsg());
                    }
                });
    }

    public void getIsAgent() {
        YmApi.getYmService()
                .getMoneyAgent()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<MyRemainMoneyInfo>getApiTransformerData())
                .compose(XApi.<MyRemainMoneyInfo>getScheduler())
                .subscribe(new ApiSubscriber<MyRemainMoneyInfo>() {
                    @Override
                    public void onSuccess(MyRemainMoneyInfo data) {
                        getV().setRemain(data);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        YMToast.showText(error.getMsg());
                    }
                });
    }


    public void getWithDrawServicePresentAgent(String money) {

        YmApi.getYmService()
                .getAgentWithdrawServicePrecent(money)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<MyServicePrecent>getApiTransformerData())
                .compose(XApi.<MyServicePrecent>getScheduler())
                .subscribe(new ApiSubscriber<MyServicePrecent>() {
                    @Override
                    public void onSuccess(MyServicePrecent data) {
//                        getV().showSureDialog(data);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        YMToast.showText(error.getMsg());
                    }
                });
    }

    public void submitDataAgent(String money) {
        YmApi.getYmService()
                .withdrawAgent(money)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<Object>getApiTransformerData())
                .compose(XApi.<Object>getScheduler())
                .subscribe(new ApiSubscriber<Object>() {
                    @Override
                    public void onSuccess(Object data) {
                        YMToast.showText("提现成功");
//                        getV().finish();
//                        getV().submitSuccess();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
//                        if (error.getCode() == 501) {
//                            YMToast.showText("用户余额不足！");
//                        } else {
                            YMToast.showText(error.getMsg());
//                        }
                    }
                });
    }

    public void getRemainMoneyAgent() {
        YmApi.getYmService()
                .getRemainMoneyAgent()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<MyRemainMoneyAgentInfo>getApiTransformerData())
                .compose(XApi.<MyRemainMoneyAgentInfo>getScheduler())
                .subscribe(new ApiSubscriber<MyRemainMoneyAgentInfo>() {
                    @Override
                    public void onSuccess(MyRemainMoneyAgentInfo data) {
//                        getV().setRemainMoney(data.getMoney());
                    }

                    @Override
                    public void onFail(ApiException error) {
                            YMToast.showText(error.getMsg());
                    }
                });
    }


}
